import {
	getUserAddressGetDefaultApi,
	getConfigHomeApi,
	getPayTypeApi,
} from '@/setupproject/INTERFACE';

import store from '@/store'
import commonConfigs from '@/setupproject/BASE_CONFIG.js'

export const MUTATIONS = {
	ADDRESS_SET_FUN: 'ADDRESS_SET_FUN',
	
	SHOW_NEW_PERSON_POPUP_SET_FUN: 'SHOW_NEW_PERSON_POPUP_SET_FUN', // 特惠专区弹框
	ONCE_RELOGIN_SET_FUN: 'ONCE_RELOGIN_SET_FUN', // 单点登录弹框
	ONCE_FIRING_SET_FUN: 'ONCE_FIRING_SET_FUN', // 启动页
	SHOW_RECHARGE_SET_FUN: 'SHOW_RECHARGE_SET_FUN', // 限额提醒
	
	SHOW_LOADING_SET_FUN: 'SHOW_LOADING_SET_FUN',

	NAV_TITLE_SET_FUN: 'NAV_TITLE_SET_FUN',
	NAV_HEIGHT_SET_FUN: 'NAV_HEIGHT_SET_FUN',
	CHECK_LIST_SET_FUN: 'CHECK_LIST_SET_FUN',

	URL_QUERY_SET_FUN: 'URL_QUERY_SET_FUN',

	RECHRGE_TYPE_SET_FUN: 'RECHRGE_TYPE_SET_FUN',

	DEVICE_INFO_SET_FUN: 'DEVICE_INFO_SET_FUN',

	THIRD_PARTY_SET_FUN: 'THIRD_PARTY_SET_FUN',

	BASE_CONFIG_SET_FUN: 'BASE_CONFIG_SET_FUN',

	ONLINE_STATUS_SET_FUN: 'ONLINE_STATUS_SET_FUN',

	LOCAL_IMAGE_STATUS_SET_FUN: 'LOCAL_IMAGE_STATUS_SET_FUN',

	SHOW_RULE_SET_FUN: 'SHOW_RULE_SET_FUN',

	INFINITE_DETAILS_SET_FUN: 'INFINITE_DETAILS_SET_FUN', 
	GLOBAL_DETAILS_SET_FUN: 'GLOBAL_DETAILS_SET_FUN',
	
	SET_WATCH_PLAT_PARAMS: 'SET_WATCH_PLAT_PARAMS',
	
	SHARE_INFO_SET_FUN: 'SHARE_INFO_SET_FUN',
	LAUNCH_MP_SET_FUN: 'LAUNCH_MP_SET_FUN',
	
	SHOW_CODE_POPUP_SET_FUN: 'SHOW_CODE_POPUP_SET_FUN', // 验证码弹框
	SHOW_COUNT_DOWN_POPUP_SET_FUN: 'SHOW_COUNT_DOWN_POPUP_SET_FUN', // 轮询倒计时弹框
	ORDER_PAY_STATUS_SET_FUN: 'ORDER_PAY_STATUS_SET_FUN', // 支付结果  
	SHOW_COUPON_SET_FUN: 'SHOW_COUPON_SET_FUN', // 新人优惠券弹窗
	
	SHOW_PRIVACY_SET_FUN: 'SHOW_PRIVACY_SET_FUN', // 购买协议 隐私条款
	ONCE_NEWS_SET_FUN: 'ONCE_NEWS_SET_FUN', // 系统通知弹框
	
	SHOW_AGREEMENT_SET_FUN: 'SHOW_AGREEMENT_SET_FUN', // 显隐协议弹框
	SHOW_GROUP_SET_FUN: 'SHOW_GROUP_SET_FUN', // 显隐协议弹框
	APP_QRCODE_SET_FUN: 'APP_QRCODE_SET_FUN', // 显隐app二维码
	WXPAY_TIP_SET_FUN: 'WXPAY_TIP_SET_FUN', // 显隐app二维码 提示语
	
	SET_DETAIL_RECHARGE_FLAG: 'SET_DETAIL_RECHARGE_FLAG', // 详情页充值标志位
	
}

export const ACTIONS = {
	ADDRESS_SET_FUN: "ADDRESS_SET_FUN",
	
	// 获取支付类型
	RECHARGE_TYPE_GET_FUN: 'RECHARGE_TYPE_GET_FUN',

	// APP获取第三方配置
	THIRD_PARTY_GET_FUN: 'THIRD_PARTY_GET_FUN',

	LOCAL_IMAGE_GET_FUN: 'LOCAL_IMAGE_GET_FUN', // APP 图片预加载
}

export default {
	name: 'commonStore',
	state: {
		address: null,
		
		showPrivacyPopup: false,
		showOnceNewsPopup: true, // 系统通知弹框
		showNewPersonPopup: true, // 特惠专区弹框
		showOnceReloginPopup: false, // 单点登录弹框
		isShowStartupPage: true, // 启动页
		showMegaAnimationPopup: true, // 合成跳过动画
		showRechargePopup: false, // 限额提醒
	
		commonAllowShowLoadings: false,

		commonNavTitle: "旺卡抽赏",
		commonNavHeight: 0,

		commonPaySortList: [1, 2, 3, 4],

		// 监听路径携带的参数，传给服务端
		commonMpQuery: {},

		// 获取支付类型
		commonPayTypeList: [],

		commonDeviceInfo: {}, // 设备信息

		commonConfigs: {}, // 项目基本配置

		commonWechatLogin: {}, // APP 微信登录参数 { wechatOpenAppId }
		commonShareMP: {}, // APP 分享小程序参数 { wechatMPShareId，wechatMPShareType，wechatMPShareWebUrl }
		commonCustomerWechat: {}, // APP 微信客服参数 { wechatKFcorpid，wechatKFcusUrl }

		commonOnline: true, // 是否提审阶段，true： 提审中

		commonLocalFileUrls: {}, // APP 图片预加载

		commonShowOrderRule: true, // 每次进入小程序，只触发一次发货规则弹框

		commonInfiniteDetail: {}, //卡牌详情
		commonGlaobalDetail: {}, //全局赏详情
		
		backAppParams: {}, // 小程序跳转回原平台携带的参数
		
		shareObjData: {
			imageUrl: '',
			content: '',
			desc: '',
			title: '',
			path: '',
		},
		
		showAgreeTxtPopup: false,
		showJoinGroupPopup: false,
		showAppQrcodePopup: false,
		wxpayTipContentPopup: '',
		showCodePopupPopup: false,
		showCountdownPopup: false,
		orderPayStatusPopup: 0,
		isNewCouponPopup: true,
		
		detailRechargeFlag: false, // 详情页充值标志位
	},
	getters: {
		showAgreeTxtPopup: state => state.showAgreeTxtPopup,
		showJoinGroupPopup: state => state.showJoinGroupPopup,
		showAppQrcodePopup: state => state.showAppQrcodePopup,
		wxpayTipContentPopup: state => state.wxpayTipContentPopup,
		showCodePopupPopup: state => state.showCodePopupPopup,
		showCountdownPopup: state => state.showCountdownPopup,
		orderPayStatusPopup: state => state.orderPayStatusPopup,
		
		address: state => state.address,

		commonAllowShowLoadings: state => state.commonAllowShowLoadings,

		// 页面标题
		commonNavTitle: state => state.commonNavTitle,
		commonNavHeight: state => state.commonNavHeight,

		commonPaySortList: state => state.commonPaySortList,

		// 监听路径参数
		commonMpQuery: state => state.commonMpQuery,

		// 获取支付类型
		commonPayTypeList: state => state.commonPayTypeList,

		// 获取手机设备信息
		commonDeviceInfo: state => state.commonDeviceInfo,

		// 项目基本配置
		commonConfigs: state => state.commonConfigs,

		commonWechatLogin: state => state.commonWechatLogin, // APP 微信登录参数 { wechatOpenAppId }
		commonShareMP: state => state.commonShareMP, // APP 分享小程序参数 { wechatMPShareId，wechatMPShareType，wechatMPShareWebUrl }
		commonCustomerWechat: state => state.commonCustomerWechat, // APP 微信客服参数 { wechatKFcorpid，wechatKFcusUrl }

		commonOnline: state => state.commonOnline, //是否提审阶段，true： 提审中

		commonLocalFileUrls: state => state.commonLocalFileUrls, // APP 图片预加载

		commonShowOrderRule: state => state.commonShowOrderRule, // 每次进入小程序，只触发一次发货规则弹框

		commonInfiniteDetail: state => state.commonInfiniteDetail, //卡牌详情
		commonGlaobalDetail: state => state.commonGlaobalDetail, //全局赏详情
		
		backAppParams: state => state.backAppParams, // 小程序跳转回原平台携带的参数
		
		shareObjData: state => state.shareObjData,
		
		
		isNewCouponPopup: state => state.isNewCouponPopup,
		showPrivacyPopup: state => state.showPrivacyPopup,
		showOnceNewsPopup: state => state.showOnceNewsPopup,
		showNewPersonPopup: state => state.showNewPersonPopup,
		showOnceReloginPopup: state => state.showOnceReloginPopup,
		isShowStartupPage: state => state.isShowStartupPage,
		showMegaAnimationPopup: state => state.showMegaAnimationPopup,
		showRechargePopup: state => state.showRechargePopup,
		
		detailRechargeFlag: state => state.detailRechargeFlag, // 详情页充值标志位
	},
	mutations: {
		[MUTATIONS.ADDRESS_SET_FUN](state, data) {
			state.address = data
		},
		
		// 微信分享
		[MUTATIONS.SHARE_INFO_SET_FUN](state, data) {
			state.shareObjData = data;
			
			// #ifdef APP-PLUS
			uni.showLoading({
				title: '',
				mask: true,
			});
		
			//分享到微信朋友
			uni.share({
				provider: "weixin",
				scene: "WXSceneSession",
				type: 5,
				title: data.title,
				imageUrl: data.imageUrl,
				miniProgram: {
					type: store.getters.commonShareMP.wechatMPShareType || commonConfigs
					.wechatMPShareType, // 0-正式版； 1-测试版； 2-体验版。 默认值为0。
					webUrl: store.getters.commonShareMP.wechatMPShareWebUrl || commonConfigs.wechatMPShareWebUrl, // 兼容低版本的网页链接
					id: store.getters.commonShareMP.wechatMPShareId || commonConfigs
					.wechatMPShareId, // 微信小程序原始id
					path: data.path, // 点击链接进入的页面
				},
				success: function(res) {
					console.log("success:" + JSON.stringify(res));
				},
				fail: function(err) {
					console.log("fail:" + JSON.stringify(err));
				},
				complete: function(comp) {
					console.log("complete:" + JSON.stringify(comp));
					uni.hideLoading();
				},
			});
			setTimeout(function() {
				uni.hideLoading();
			}, 1200);
			// #endif
		},
		
		[MUTATIONS.SHOW_AGREEMENT_SET_FUN](state, data) {
			state.showAgreeTxtPopup = data
		},
		[MUTATIONS.SHOW_GROUP_SET_FUN](state, data) {
			state.showJoinGroupPopup = data
		},
		[MUTATIONS.APP_QRCODE_SET_FUN](state, data) {
			state.showAppQrcodePopup = data
		},
		[MUTATIONS.WXPAY_TIP_SET_FUN](state, data) {
			state.wxpayTipContentPopup = data
		},
		// 优惠券弹窗
		[MUTATIONS.SHOW_COUPON_SET_FUN](state, data) {
			state.isNewCouponPopup = data
		},
		// 验证码弹框
		[MUTATIONS.SHOW_CODE_POPUP_SET_FUN](state, data) {
			state.showCodePopupPopup = data
		},
		// 轮询倒计时弹框
		[MUTATIONS.SHOW_COUNT_DOWN_POPUP_SET_FUN](state, data) {
			state.showCountdownPopup = data
		},
		// 购买协议隐私条款
		[MUTATIONS.SHOW_PRIVACY_SET_FUN](state, data) {
			state.showPrivacyPopup = data
		},
		// 优惠券弹窗
		[MUTATIONS.ONCE_NEWS_SET_FUN](state, data) {
			state.showOnceNewsPopup = data
		},
		// 特惠专区弹框
		[MUTATIONS.SHOW_NEW_PERSON_POPUP_SET_FUN](state, data) {
			state.showNewPersonPopup = data
		},
		// 单点登录弹框
		[MUTATIONS.ONCE_RELOGIN_SET_FUN](state, data) {
			state.showOnceReloginPopup = data
		},
		// 启动页
		[MUTATIONS.ONCE_FIRING_SET_FUN](state, data) {
			state.isShowStartupPage = data
		},
		[MUTATIONS.SHOW_RECHARGE_SET_FUN](state, data) {
			state.showRechargePopup = data
		},
		
		// APP跳转小程序
		[MUTATIONS.LAUNCH_MP_SET_FUN](state, data) {
			// #ifdef APP-PLUS
			uni.showLoading({
				title: '',
				mask: true,
			});
		
			plus.share.getServices(res => {
				let shareAPPmp = res.find(i => i.id === 'weixin');
				if (shareAPPmp) {
					shareAPPmp.launchMiniProgram({
						path: data.path,
						id: data.appid,
						type: data.type || 0,
					}, res => {
						uni.hideLoading();
					}, err => {
						uni.hideLoading();
					})
				} else {
					// 没有获取到微信分享服务
					uni.showToast({
						icon: 'none',
						title: "未安装微信,无法打开对应小程序!"
					})
				}
			}, err => {
				uni.showToast({
					icon: 'none',
					title: "获取服务失败：" + JSON.stringify(err)
				})
			});
		
			setTimeout(function() {
				uni.hideLoading();
			}, 1800);
			// #endif
		},
		
		// APP获取第三方配置
		[MUTATIONS.THIRD_PARTY_SET_FUN](state, data) {
			if (data.shareMP) {
				data.shareMP.live_status = data.live_status ? true : false;
				state.commonShareMP = data.shareMP;
			}
		
			if (data.customerServiceWechat) {
				state.commonCustomerWechat = data.customerServiceWechat;
			}
			
			if (data.WechatLogin) {
				state.commonWechatLogin = data.WechatLogin;
			}
		},
		
		// 设置项目基本配置
		[MUTATIONS.BASE_CONFIG_SET_FUN](state, data) {
			state.commonConfigs = data
		},
		
		// 是否提审阶段，false： 提审中
		[MUTATIONS.ONLINE_STATUS_SET_FUN](state, data) {
			state.commonOnline = data
		},
		
		// APP 图片预加载
		[MUTATIONS.LOCAL_IMAGE_STATUS_SET_FUN](state, data) {
			state.commonLocalFileUrls[data.key] = data.url;
		},
		
		[MUTATIONS.SHOW_RULE_SET_FUN](state, data) {
			state.commonShowOrderRule = data
		},
		
		// 小程序跳转回原平台携带的参数
		[MUTATIONS.SET_WATCH_PLAT_PARAMS](state, data) {
			state.backAppParams = data
		},
		
		// loading
		[MUTATIONS.SHOW_LOADING_SET_FUN](state, data) {
			state.commonAllowShowLoadings = data
		},

		// 页面标题栏 相关store数据
		[MUTATIONS.NAV_TITLE_SET_FUN](state, data) {
			state.commonNavTitle = data
		},
		[MUTATIONS.NAV_HEIGHT_SET_FUN](state, data) {
			state.commonNavHeight = data
		},

		[MUTATIONS.CHECK_LIST_SET_FUN](state, data) {
			state.commonPaySortList = data
		},

		// 监听路径携带的参数，传给服务端
		[MUTATIONS.URL_QUERY_SET_FUN](state, data) {
			state.commonMpQuery = data
		},

		// 获取支付类型
		[MUTATIONS.RECHRGE_TYPE_SET_FUN](state, data = {}) {
			if(data) state.commonPayTypeList = data.interfaces || [];
		},

		// 获取手机设备信息
		[MUTATIONS.DEVICE_INFO_SET_FUN](state, data) {
			state.commonDeviceInfo = data
		},

		[MUTATIONS.INFINITE_DETAILS_SET_FUN](state, data) {
			state.commonInfiniteDetail = data
		},
		[MUTATIONS.GLOBAL_DETAILS_SET_FUN](state, data) {
			state.commonGlaobalDetail = data
		},
		
		// 详情页充值标志位
		[MUTATIONS.SET_DETAIL_RECHARGE_FLAG](state, data) {
			state.detailRechargeFlag = data
		},
	},
	actions: {
		async [ACTIONS.ADDRESS_SET_FUN](store) {
			const {
				code,
				data
			} = await getUserAddressGetDefaultApi()
			if (code === 200) {
				store.commit('ADDRESS_SET_FUN', data)
			}
		},
		
		async [ACTIONS.RECHARGE_TYPE_GET_FUN](store, data) {
			const {
				data: result
			} = await getPayTypeApi(data)
			store.commit(MUTATIONS.RECHRGE_TYPE_SET_FUN, result);
		},

		async [ACTIONS.THIRD_PARTY_GET_FUN](store, data) {
			const {
				data: result
			} = await getConfigHomeApi(data)
			store.commit(MUTATIONS.THIRD_PARTY_SET_FUN, result);
		},
	}
}